/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package etherblocks.mods.shooter.states;

import com.jme3.app.Application;
import com.jme3.app.state.AppStateManager;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseButtonTrigger;
import etherblocks.engine.states.*;
import etherblocks.game.entitysystem.*;
import etherblocks.mods.shooter.entitysystem.components.*;
import etherblocks.mods.shooter.messages.*;

/**
 *
 * @author Carl
 */
public class ShooterPlayerAppState extends BaseAppState implements ActionListener{

    public ShooterPlayerAppState(){
        
    }

    @Override
    public void initialize(AppStateManager stateManager, Application application){
        super.initialize(stateManager, application);
        mainApplication.getInputManager().addMapping("shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
        mainApplication.getInputManager().addMapping("spawn", new KeyTrigger(KeyInput.KEY_ADD));
        mainApplication.getInputManager().addListener(this, new String[]{
            "shoot","spawn"
        });
    }
    
    @Override
    public void onAction(String actionName, boolean value, float lastTimePerFrame){
        NetworkAppState networkAppState = getAppState(NetworkAppState.class);
        if(actionName.equals("shoot") && value){
            getAppState(WeaponAppState.class).getWeaponObject().shoot();
            Entity entity = getAppState(EntitySystemAppState.class).getTargetedEntity();
            if((entity != null) && (entity.getComponent(ShootableComponent.class) != null)){
                networkAppState.getNetworkClient().sendMessage(new Message_EntityShot(entity.getID()));
            }
        }
        else if(actionName.equals("spawn") && value){
            networkAppState.getNetworkClient().sendMessage(new Message_SpawnCommand());
        }
    }
}
